{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "code_nn_only_using_numpy.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyOTktjwLXslQFf9W4kuuEE+",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/SummerLife/Embedded-ML/blob/master/project/08_code_nn_from_scratch/code_nn_only_using_numpy.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FTcVxIuIAriD",
        "colab_type": "text"
      },
      "source": [
        "## Forward Propagation Using Numpy"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cB4XDZ8i9Z1a",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np"
      ],
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nHo-YNiH-Ug0",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def sigmoid(x):\n",
        "    return 1 / (1 + np.exp(-x))\n",
        "\n",
        "def identity_function(x):\n",
        "    return x"
      ],
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TrSZBns4-XNf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def init_network():\n",
        "    network = {}\n",
        "    network['W1'] = np.array([[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]])\n",
        "    network['b1'] = np.array([0.1, 0.2, 0.3])\n",
        "    network['W2'] = np.array([[0.1, 0.4],[0.2, 0.5],[0.3, 0.6]])\n",
        "    network['b2'] = np.array([0.1, 0.2])\n",
        "    network['W3'] = np.array([[0.1, 0.3],[0.2, 0.4]])\n",
        "    network['b3'] = np.array([0.1, 0.2])\n",
        "    return network"
      ],
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "14_NjYpn_het",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def forward(network, x):\n",
        "    W1, W2, W3 = network['W1'], network['W2'], network['W3']\n",
        "    b1, b2, b3 = network['b1'], network['b2'], network['b3']\n",
        "\n",
        "    a1 = np.dot(x, W1) + b1\n",
        "    z1 = sigmoid(a1)\n",
        "    a2 = np.dot(z1, W2) + b2\n",
        "    z2 = sigmoid(a2)\n",
        "    a3 = np.dot(z2, W3) + b3\n",
        "    y = identity_function(a3)\n",
        "\n",
        "    return y"
      ],
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ECZ16LUyARy-",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "dac261db-a2ec-4387-cfe7-6b36c7cbcffa"
      },
      "source": [
        "network = init_network()\n",
        "x = np.array([1.0, 0.5])\n",
        "y = forward(network, x)\n",
        "print(y)"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[0.31682708 0.69627909]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yT4Wv2vuAd2j",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}